<?php
namespace common\models\member;

use common\models\common\BaseModel;
use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use common\enums\StatusEnum;
use common\helpers\RegularHelper;

class MemberStore extends BaseModel
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%member_store}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['member_id', 'store_id'], 'required'],
            [['member_id','store_id', 'status', 'created_at','user_integral','last_time'], 'integer'],
            [['user_money', 'accumulate_money', 'frozen_money'], 'number'],
            [['last_ip'], 'string', 'max' => 16],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            're_id' => 'ID',
            'member_id'=>'商户ID',
            'store_id' => '账号',
            'user_money' => '余额',
            'accumulate_money' => '累计金额',
            'frozen_money' => '冻结金额',
            'user_integral' => '积分',
            'last_time' => '最后一次登录时间',
            'last_ip' => '最后一次登录ip',
            'status' => '状态',
            'created_at' => '创建时间',
        ];
    }

    /**
     * 场景
     *
     * @return array
     */
    public function scenarios()
    {
        return [
//            'backendCreate' => ['username', 'password_hash'],
            'default' => array_keys($this->attributeLabels()),
        ];
    }

    /**
     * @param bool $insert
     * @return bool
     * @throws \yii\base\Exception
     */
    public function beforeSave($insert)
    {
        if ($this->isNewRecord)
        {
            $this->last_ip = Yii::$app->request->getUserIP();
            $this->last_time = time();
//            $this->auth_key = Yii::$app->security->generateRandomString();
        }

        return parent::beforeSave($insert);
    }

    /**
     * @return array
     */
    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
            ],
        ];
    }
}
